package com.gouqi.mall.service.impl;

import com.gouqi.mall.domain.ResultJson;
import com.gouqi.mall.domain.User;
import com.gouqi.mall.mapper.UserMapper;
import com.gouqi.mall.service.UserService;
import com.gouqi.mall.utils.DateUtils;
import com.gouqi.mall.utils.MD5Util;
import com.gouqi.mall.utils.StringUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * UserService实现类
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    private JSONObject json = new JSONObject();
    private ResultJson result = new ResultJson();

    @Override
    public Integer insertUser(User user) {
        //设置密码为32位
        String password = MD5Util.getMD5(user.getPassword());
        user.setPassword(password);
        /**
         * 设置userId:
         *   userId生成规则：用户账号+当前时间+盐 = 生成唯一32位字符串存数据
         */
        String account = user.getAccount();
        // 账户为空，以手机号位账户
        if (account == null) {
            user.setAccount(user.getPhone());
        }
        //当前时间
        String currentTime = String.valueOf(new Date()) ;
        // user.setCreateTime(DateUtils.dateToTimeStamp(new Date()));
        user.setCreateTime(currentTime);
        //userID生成规则
        String strRules = user.getAccount().concat(currentTime);
        System.out.println("生成规则字符串："+strRules);
        String userId = MD5Util.getMD5(strRules);
        System.out.println("生成的userId："+userId);
        user.setUserId(userId);
        return userMapper.insertUser(user);
    }


    @Override
    public Integer getUserIdByAccount(String account) {
        return userMapper.getUserIdByAccount(account);
    }

    @Override
    public Boolean updateUserById(User user) {
        return userMapper.updateUserById(user);
    }

    @Override
    public User isHasUser(String account, String password) {
        return userMapper.isHasUser(account,password);
    }

    @Override
    public User userIsExist(String phone, String password) {
        return userMapper.userIsExist(phone,password) ;
    }

    @Override
    public Integer registerByPhoneAndGetId(User user) {
        /**
         * 首先生成 userId和account:
         *   userId生成规则：手机号+当前注册时间+盐 = 生成MD5(32)位标识
         *   account生成规则：可以是中文也可以是英文，类似于昵称
         */
        user.setAccount(user.getPhone());
        //当前时间
        String currentTime = String.valueOf(new Date()) ;
        //userID生成规则
        String strRules = user.getAccount().concat(currentTime);
        System.out.println("生成规则字符串："+strRules);
        String userId = MD5Util.getMD5(strRules);
        //String userId = MD5Util.getMD5(user.getPhone() + new Date().toString());
        user.setUserId(userId);
        // user.setCreateTime(DateUtils.dateToTimeStamp(new Date()));
        user.setCreateTime(String.valueOf(new Date()));
        //设置注册时间
        return userMapper.registerByPhoneAndGetId(user);
    }

    @Override
    public String queryUserDetails(String userId) {
        User user = userMapper.queryUserDetails(userId);
        System.out.println("查询到的用户信息为："+user.toString());
        if (user != null) {
            result.setStatus(200);
            result.setCode(200);
            result.setMessage("获取用户信息成功！");
            result.setResultJson(user);
        } else {
            result.setStatus(404);
            result.setCode(204);
            result.setMessage("获取用户信息失败！");
            result.setResultJson(user);
        }
        json.put("result",result);
        return json.toString();
    }
}
